package cn.qidd.keep.domain.web;

import cn.qidd.keep.domain.core.Constants;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.TimeUnit;

/**
 * Created by sunyanping on 2018/9/13
 */
public class BaseController {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    protected UserVO getUserByToken(String token) {
        if (StringUtils.isBlank(token)) throw new TokenException();
        Boolean expire = stringRedisTemplate.expire(token, Constants.TOKEN_EXPIRED_TIME, TimeUnit.MINUTES);
        if (expire == null || !expire) throw new TokenException();
        String tokenString = stringRedisTemplate.opsForValue().get(token);
        return JSONObject.parseObject(tokenString, UserVO.class);
    }
}
